﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataModel;
using Mvc4.Others;
using System.Web.Helpers;
using DataAccess.Datatypes;
using BussinesLayer.Services;

using Mvc4.Models;

namespace Mvc4.Controllers
{
    public class HomeSiteController : Controller
    {
        private GoodServices gs = new GoodServices();
        
        //
        // GET: /HomeSite/

        public ActionResult Index(int siteId, string siteName)
        {
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName;
            ViewBag.SiteId = siteId;
            IEnumerable<Good> goods = gs.GetBestGoodsHome(siteId); //Devuelve los n bienes mejor rankeados
            return View(new Search(goods));
        }

        public ActionResult Search(Search search, int siteId, string siteName)
        {
            ViewBag.Name = siteName;
            ViewBag.SiteName = siteName + " - Search";
            ViewBag.SiteId = siteId;
            IEnumerable<Good> goods = gs.SearchGoods(siteId, search.GoodName, search.From, search.To);
            return View(new Search(goods));
        }

        public void GetBigImage(int goodId)
        {
            byte[] image = gs.GetGoodBigImage(goodId);
            WebImage webImage;
            if (image == null)
            {
                webImage = new WebImage("~/images/no_image.jpg");
                webImage.FileName = "no_image.jpg";
            }
            else
            {
                webImage = new WebImage(image);
                webImage.FileName = "good.jpg";
            }
            webImage.Resize(1170, 500);
            webImage.Write();
        }

        public void GetImage(int goodId)
        {
            byte[] image = gs.GetGoodImage(goodId);
            WebImage webImage;
            if (image == null)
            {
                webImage = new WebImage("~/images/no_image.jpg");
                webImage.FileName = "no_image.jpg";
            }
            else
            {
                webImage = new WebImage(image);
                webImage.FileName = "good.jpg";
            }
            webImage.Resize(150, 150);
            webImage.Write();
        }

        public void GetImageGood(int goodId)
        {
            byte[] image = gs.GetGoodImage(goodId);
            WebImage webImage;
            if (image == null)
            {
                webImage = new WebImage("~/images/no_image.jpg");
                webImage.FileName = "no_image.jpg";
            }
            else
            {
                webImage = new WebImage(image);
                webImage.FileName = "good.jpg";
            }
            webImage.Resize(300, 300);
            webImage.Write();
        }
    }
}
